1 package net.sourceforge.simplegamenet.dice;
2
3 import java.io.Serializable;
4 import javax.swing.*;
5 import net.sourceforge.simplegamenet.specs.gui.GamePanel;
6 import net.sourceforge.simplegamenet.specs.gui.UtilityPanelFactory;
7 import net.sourceforge.simplegamenet.specs.model.ClientEngine;
8 import net.sourceforge.simplegamenet.specs.model.GamePlayerClient;
9
10 public class DicePlayerClient extends GamePlayerClient implements DicePacketCodes,
11 DiceSettingOptions {
12
13 private DicePanel dicePanel;
14 private Integer nextPlayer;
15 private Integer thisPlayer;
16 private int nextPlayerIndex;
17 private Integer[] diceParticipantIDs;
18 private DiceScore diceScore = null;
19
20 public DicePlayerClient(ClientEngine clientEngine) {
21 super(clientEngine);
22 }
23
24 public GamePanel createGamePanel(UtilityPanelFactory utilityPanelFactory) {
25 dicePanel = new DicePanel(this);
26 return dicePanel;
27 }
28
29 public void receiveData(Serializable data) {
30 DicePacket dicePacket = (DicePacket) data;
31 int[] filledScore = new int[3];
32 switch (dicePacket.dicePacketCode) {
33 case GAME_FIRST_SCORE:
34 if (!(dicePacket.diceData instanceof DiceScore)) {
35 clientEngine.leaveProtocolViolatingServer();
36 return;
37 }
38 diceScore = (DiceScore) dicePacket.diceData;
39 break;
40 case GAME_FIRST_PLAYER:
41 diceParticipantIDs = (Integer[]) dicePacket.diceData;
42 nextPlayerIndex = 0;
43 nextPlayer = diceParticipantIDs[nextPlayerIndex];
44 dicePanel.setupDicePanels();
45 dicePanel.enablePanelForPlay(nextPlayer);
46 break;
47 case GAME_NEXT_PLAYER:
48 nextPlayerIndex = ((Integer) dicePacket.diceData).intValue();
49 nextPlayer = diceParticipantIDs[nextPlayerIndex];
50 dicePanel.enablePanelForPlay(nextPlayer);
51 break;
52 case TURN_DICE_ROLLED:
53 dicePanel.setRolledDice((int[]) dicePacket.diceData);
54 break;
55 case TURN_DICE_HELD:
56 dicePanel.highlightDice(((Integer) dicePacket.diceData).intValue());
57 break;
58 case TURN_SCORE_FILLED:
59 filledScore = (int[]) dicePacket.diceData;
60 thisPlayer = diceParticipantIDs[filledScore[0]];
61 if (filledScore[2] == 0) {
62 int answer = dicePanel.askQuestion();
63 if (answer == JOptionPane.YES_OPTION) {
64 dicePacket = new DicePacket(TURN_CONFIRM_ZERO, dicePacket.diceData);
65 sendDicePacket(dicePacket);
66 } else {
67 dicePanel.enableScorePlayPanel();
68 }
69 } else {
70 diceScore.setPlayerScore(thisPlayer, filledScore[1], filledScore[2]);
71 dicePanel.setFilledScore(filledScore);
72 if (filledScore[1] == 10 || filledScore[1] == 11
73 || filledScore[1] == 12 || filledScore[1] == 13) {
74 diceScore.adjustSettingValues(filledScore[1]);
75 dicePanel.adjustSettingValues(filledScore[1], diceScore.getSettingValues());
76 }
77 int[] playerTotals = diceScore.getPlayerTotals(thisPlayer);
78 dicePanel.setTotalScores(playerTotals);
79 if (thisPlayer.equals(getPlayerID())) {
80 if (!diceScore.getPlayerBonusFilled(thisPlayer)) {
81 checkBonus();
82 }
83 sendDicePacket(
84 new DicePacket(GAME_NEXT_PLAYER, new Integer(nextPlayerIndex)));
85 }
86 }
87 break;
88 case TURN_ZERO_CONFIRMED:
89 filledScore = (int[]) dicePacket.diceData;
90 diceScore.setPlayerScore(nextPlayer, filledScore[1], filledScore[2]);
91 dicePanel.setFilledScore(filledScore);
92 if (nextPlayer.equals(getPlayerID())) {
93 sendDicePacket(new DicePacket(GAME_NEXT_PLAYER, new Integer(nextPlayerIndex)));
94 }
95 break;
96 case BONUS_CHECKED:
97 filledScore = (int[]) dicePacket.diceData;
98 diceScore.setPlayerScore(thisPlayer, filledScore[1], filledScore[2]);
99 diceScore.setPlayerBonusFilled(thisPlayer, true);
100 diceScore.adjustSettingValues(filledScore[1]);
101 dicePanel.adjustSettingValues(filledScore[1], diceScore.getSettingValues());
102 dicePanel.setFilledScore(filledScore);
103 dicePanel.setTotalScores(diceScore.getPlayerTotals(thisPlayer));
104 break;
105 default :
106 System.out.println("Unrecognized packet");
107 }
108 }
109
110 public void checkBonus() {
111 int bonus = diceScore.checkBonus(getPlayerID());
112 if (bonus != 0 && !(diceScore.getSettingValues()[2] == 0)) {
113 int[] fillBonus = new int[3];
114 fillBonus[0] = getPlayerIDIndex();
115 fillBonus[1] = 6;
116 fillBonus[2] = bonus;
117 DicePacket dicePacket = new DicePacket(BONUS_CHECK, fillBonus);
118 sendDicePacket(dicePacket);
119 } else if (bonus == 0
120 && dicePanel.getDiceScorePlayPanel().checkAllDisabled(getPlayerIDIndex())
121 && !(diceScore.getSettingValues()[2] == 0)) {
122 int[] fillBonus = new int[3];
123 fillBonus[0] = getPlayerIDIndex();
124 fillBonus[1] = 6;
125 fillBonus[2] = -1;
126 DicePacket dicePacket = new DicePacket(BONUS_CHECK, fillBonus);
127 sendDicePacket(dicePacket);
128 } else if (bonus == 0 && diceScore.getSettingValues()[2] == 0) {
129 int[] fillBonus = new int[3];
130 fillBonus[0] = getPlayerIDIndex();
131 fillBonus[1] = 6;
132 fillBonus[2] = bonus;
133 DicePacket dicePacket = new DicePacket(BONUS_CHECK, fillBonus);
134 sendDicePacket(dicePacket);
135 }
136 }
137
138 public void sendDicePacket(DicePacket dicePacket) {
139 clientEngine.sendData(dicePacket);
140 }
141
142 public int[] getSettingValues() {
143 return diceScore.getSettingValues();
144 }
145
146 public int[] getAmountValues() {
147 return diceScore.getAmountValues();
148 }
149
150 public int getDicePlayerAmount() {
151 return clientEngine.getPlayerSettingsMap().getParticipantsAmount();
152 }
153
154 public Integer getPlayerID() {
155 return clientEngine.getPlayerID();
156 }
157
158 public int getPlayerIDIndex() {
159 for (int i = 0; i < diceParticipantIDs.length; i++) {
160 if (getPlayerID().equals(diceParticipantIDs[i])) {
161 return i;
162 }
163 }
164 return 0;
165 }
166
167 public Integer[] getDiceParticipantIDs() {
168 return diceParticipantIDs;
169 }
170
171 public String getDicePlayerNickname(Integer dicePlayerID) {
172 return clientEngine.getPlayerSettingsMap()
173 .getPlayerSettings(dicePlayerID).getNickname();
174 }
175
176 public Integer getNextPlayer() {
177 return nextPlayer;
178 }
179
180 public void gameStopping() {
181 dicePanel.disablePanel();
182 dicePanel.showWinner(diceScore.getWinner());
183 }
184
185 public void gameAborting() {
186 dicePanel.disablePanel();
187 dicePanel.showWinner(diceScore.getWinner());
188 }
189
190 public boolean containsPlayerID(Integer playerID) {
191 return clientEngine.getPlayerSettingsMap().containsPlayerID(playerID);
192 }
193
194 }